package com.aiti.lulian.service.newCard;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.BaseService;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.lulian.constant.NewCardBusinessConstant;
import com.aiti.lulian.dto.newCard.CardShareConfig;
import com.aiti.lulian.dto.newCard.UpdateCardForwardDto;
import com.aiti.lulian.entity.CardEnterpriseTemplate;
import com.aiti.lulian.entity.CardUserCardForwardCoverOption;
import com.aiti.lulian.entity.newCard.CardAudit;
import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.mapper.CardEnterpriseTemplateMapper;
import com.aiti.lulian.mapper.newCard.CardAuditMapper;
import com.aiti.lulian.mapper.newCard.CardDefaultTemplateMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.mapper.newCard.CardShareConfigMapper;
import com.aiti.lulian.service.CardUserCardForwardCoverOptionService;
import com.aiti.lulian.vo.newCard.CardForwordVo;
import com.aiti.lulian.vo.newCard.CoverOptionUrlVo;
import com.aiti.lulian.vo.newCard.CoverOptionWrapperUrlVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * auth fcb
 * date 2025-09-11 17:55
 **/
@Service
@Slf4j
public class CardShareConfigService extends BaseSerivceImpl<CardShareConfigMapper, CardShareConfig> {

    @Autowired
    private CardAuditMapper cardAuditMapper;
    @Autowired
    private CardPersonalBaseInfoMapper cardPersonalBaseInfoMapper;
    @Autowired
    private CardDefaultTemplateMapper cardDefaultTemplateMapper;
    @Autowired
    private CardEnterpriseTemplateMapper cardEnterpriseTemplateMapper;
    @Autowired
    private CardUserCardForwardCoverOptionService cardUserCardForwardCoverOptionService;

    public CardShareConfig buildCardShareConfig(String enterpriseId, String templateId) {
        CardShareConfig cardShareConfig = new CardShareConfig();
        cardShareConfig.setBaseId(IdUtil.simpleUUID());
        cardShareConfig.setTemplateId(templateId);
        cardShareConfig.setEnterpriseId(enterpriseId);
        cardShareConfig.setHelloWordType(0);
        cardShareConfig.setHelloWord(cardDefaultTemplateMapper.queryDefaultHelloWord());
        cardShareConfig.setPermission(2);
        cardShareConfig.setCoverUrl(cardDefaultTemplateMapper.queryDefaultCoverUrl());
        cardShareConfig.setCoverUrlBaseId(cardDefaultTemplateMapper.queryDefaultCoverUrlBaseId());
        cardShareConfig.setType(cardDefaultTemplateMapper.queryDefaultType());
        return cardShareConfig;
    }

    /**
     * 查询默认的封面图片
     *
     * @return
     */
    public CoverOptionWrapperUrlVo queryDefaultCoverUrl(String templateId) {
        if(StrUtil.isBlank(templateId)) {
            throw new BusinessException("模板id不能为空");
        }
        CoverOptionWrapperUrlVo coverOptionWrapperUrlVo = new CoverOptionWrapperUrlVo();
        List<CoverOptionUrlVo> coverOptionUrlVos = this.baseMapper.queryDefaultCoverUrl();
        coverOptionWrapperUrlVo.setCoverOptionUrlList(coverOptionUrlVos);
        CardEnterpriseTemplate cardEnterpriseTemplate = cardEnterpriseTemplateMapper.queryEnterpriseTemplateInfo(templateId);
        coverOptionWrapperUrlVo.setTemplatePic(cardEnterpriseTemplate.getTemplatePic());
        CardShareConfig cardShareConfig = this.baseMapper.queryCardShareConfigByTemplateId(templateId);
        coverOptionWrapperUrlVo.setShareConfigCoverUrlBaseId(cardShareConfig.getCoverUrlBaseId());
        return coverOptionWrapperUrlVo;
    }

    /**
     * 查询模板的分享配置信息
     * @param enterpriseId
     * @param templateId
     * @return
     */
    public CardShareConfig queryCardShareConfig(String enterpriseId, String templateId) {
        CardShareConfig cardShareConfig = this.baseMapper.queryCardShareConfig(enterpriseId, templateId);
        return cardShareConfig;
    }

    /**
     * 修改模板的分享配置信息
     * @param cardShareConfig
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCardShareConfig(CardShareConfig cardShareConfig, String userId) {
        this.baseMapper.updateById(cardShareConfig);
        // 根据权限判断是否需要修改该模板下所有名片的封面和招呼语
        if(cardShareConfig.getPermission() != null && cardShareConfig.getPermission() == 1) {
            // 获取该模板下所有名片的id
            List<String> cardIdList = cardAuditMapper.queryCardIdList(cardShareConfig.getTemplateId(), null);
            if(CollectionUtil.isNotEmpty(cardIdList)) {
                this.baseMapper.updateCardShareConfig(cardIdList, cardShareConfig.getCoverUrl(), cardShareConfig.getHelloWord(), userId, cardShareConfig.getHelloWordType(), cardShareConfig.getType(), cardShareConfig.getCoverUrlBaseId());
            }
        }
    }

    /**
     * 获取名片转发的封面图片
     * @param cardId
     * @return
     */
    public CardForwordVo queryCardForwardInfo(String cardId) {
        CardForwordVo cardForwordVo = this.baseMapper.queryCardForwordInfo(cardId);
        CardAudit cardAudit = cardAuditMapper.queryCardAuditInfoByCardId(cardId);
        Integer permission = this.baseMapper.queryCardShareConfigPermission(cardAudit.getTemplateId());
        cardForwordVo.setPermission( permission);
        return cardForwordVo;
    }

    /**
     * 修改名片转发信息
     * @param updateCardForwardDto
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCardForwardInfo(UpdateCardForwardDto updateCardForwardDto) {
        if(StrUtil.isBlank(updateCardForwardDto.getCardId())) {
            throw new BusinessException("名片id不能为空");
        }
        CardBaseInfo cardBaseInfo = cardPersonalBaseInfoMapper.selectById(updateCardForwardDto.getCardId());
        if(StrUtil.isEmpty(cardBaseInfo.getTemplateId())) {
            throw new BusinessException("未查询到名片所属模板");
        }
        // 查询当前名片的转发配置
        CardShareConfig cardShareConfig = this.baseMapper.queryCardShareConfig(cardBaseInfo.getEnterpriseId(), cardBaseInfo.getTemplateId());
        if(cardShareConfig == null) {
            throw new BusinessException("未查询到名片所属模板的转发配置");
        }
        if(cardShareConfig.getPermission() == 1) {
            throw new BusinessException("当前配置只有管理员可修改");
        }
        if(StrUtil.isBlank(updateCardForwardDto.getCoverUrlBaseId()) || "null".equals(updateCardForwardDto.getCoverUrlBaseId())) {
            updateCardForwardDto.setCoverUrlBaseId(null);
        }
        this.baseMapper.updateCardForwardInfo(updateCardForwardDto.getCardId(),  updateCardForwardDto.getHelloWord(),updateCardForwardDto.getCoverUrl(), updateCardForwardDto.getUserId(), updateCardForwardDto.getHelloWordType(), updateCardForwardDto.getType(), updateCardForwardDto.getCoverUrlBaseId());
    }

    /**
     * 修改名片的转发图片
     * @param updateCardForwardDto
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCardForwardUrlByCardId(UpdateCardForwardDto updateCardForwardDto) {
        if(StrUtil.isBlank(updateCardForwardDto.getCardId()) || StrUtil.isBlank(updateCardForwardDto.getCoverUrl())) {
            throw new BusinessException("参数不能为空");
        }
        this.baseMapper.updateCoverUrlByCardId(updateCardForwardDto.getCardId(), updateCardForwardDto.getCoverUrl());
    }


}
